package com.yu.xcx.controller;

import com.yu.xcx.dto.Result;
import com.yu.xcx.entity.Shop;
import com.yu.xcx.service.IShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private UploadController uploadController;

    @Resource
    public IShopService shopService;

    /**
     * 根据id查询商铺信息
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/of/id")
    public Result queryShopById(@RequestParam( "id") Integer id){
        return shopService.queryById(id);
    }


    /**
     * 新增商品信息
     * @param shop 商品数据
     * @return 商品id
     */
    @PostMapping("/add")
    public Result saveShop(Shop shop) {
        // 写入数据库
        if(shopService.saveShop(shop)){
            return Result.ok("添加商品成功");
        }else{
            return Result.fail("添加商品失败");
        }

    }

    /**
     * 更新商品信息
     * @param shop 商品数据
     * @return 无
     */
    @PutMapping
    public Result updateShop(@RequestBody Shop shop) {
        // 写入数据库
        return shopService.updateShop(shop);
    }


    /**
     * 根据商品类型分页查询商品信息
     * @param typeId 商品类型
     * @param pagenum 页码
     * @return 商铺列表
     */
    @GetMapping("/of/type")
    public Result queryShopByType(Integer typeId, Integer pagenum) {
        // 根据类型分页查询
        return shopService.queryByType(typeId,pagenum);
    }

    /**
     * 根据商品类型分页查询商品信息
     * @param name 商品姓名
     * @param pagenum 页码
     * @return 商铺列表
     */
    @GetMapping("/of/name")
    public Result queryShopByName(String name, @RequestParam( value = "pagenum",defaultValue = "1") Integer pagenum){
        return shopService.queryByName(name, pagenum);
    }

    @GetMapping("/getGoodsList")
    public Result getGoodsList(@RequestParam("kw") String name,
                               @RequestParam("typeId") Integer typeId,
                               @RequestParam(value = "pagenum") Integer pagenum){

        if (!name.isEmpty()){
            return queryShopByName(name,pagenum);
        }else if (typeId != 0){
            return queryShopByType(typeId,pagenum);
        }else {
            return Result.fail("不可以传空值！");
        }
    }

    //返回所有用户的信息
    @GetMapping("/getAll")
    public Result getAllShops(){
        return shopService.getAllShop();
    }

    //根据id删除商品
    @PostMapping("/delete")
    public Result deleteById(@RequestParam ("shop_id") Integer shopId){
        return shopService.deleteById(shopId);
    }

    //根据用户展示商品
    @GetMapping("/of/user")
    public Result queryShopByUser(@RequestParam ("user_id") Integer userId, @RequestParam( value = "pagenum",defaultValue = "1") Integer pagenum){
        return shopService.queryByUser(userId,pagenum);
    }
}
